#include "CocosItemList.h"

using namespace cocos2d;

CCocosItem *CCocosItem::CreateItem(const char *szBackName, bool bUseFrame)
{
	CCocosItem *pNewItem;
	if(bUseFrame)
	{
		pNewItem = CCocosItem::spriteWithSpriteFrameName(szBackName);
	}
	else
	{
		pNewItem = CCocosItem::spriteWithFile(szBackName);
	}

	return pNewItem;
	
}



void CCocosItem::SetIsEnable(bool bEnable)
{
	for(VectorBtn::iterator iter = m_vBtns.begin(); iter != m_vBtns.end(); iter++)
	{
		(*iter)->SetIsEnable(bEnable);
	}
}

void CCocosItem::DisableTouches()
{
	for(VectorBtn::iterator iter = m_vBtns.begin(); iter != m_vBtns.end(); iter++)
	{
		(*iter)->Reset();
	}
}
CCocosItemList* CCocosItemList::CreateItemList(const cocos2d::CCRect &rect)
{
	CCocosItemList *pNewList = CCocosItemList::node();
	pNewList->InitState(rect);
	return pNewList;
}





CCocosItemList::CCocosItemList(void)
	: m_startY(0)
	, m_curSpeed(0)
	, m_lastY(0)
	, m_listHeight(0)
{
	
}

CCocosItemList::~CCocosItemList(void)
{
	
}

void CCocosItemList::InitState(const cocos2d::CCRect &rect)
{
	m_rc = rect;
	m_startY = m_rc.origin.y + m_rc.size.height;
}

void CCocosItemList::ResortItems()
{

	if(m_startY - m_listHeight > m_rc.origin.y)
	{
		m_startY = m_rc.origin.y + m_listHeight;
	}
	else if(m_startY < m_rc.origin.y + m_rc.size.height)
	{
		m_startY = m_rc.origin.y + m_rc.size.height;
	}

	float startY = m_startY;
	float lastHeight = 0;
	for(ListItem::iterator iter = m_listItems.begin(); iter != m_listItems.end(); iter++)
	{
		CCocosItem *pItem = *iter;
		if(pItem->getIsVisible())
		{
			startY -= (pItem->getContentSize().height + lastHeight)*0.5f;
			pItem->setPosition(ccp(CCRect::CCRectGetMidX(m_rc), startY));
			lastHeight = pItem->getContentSize().height;
		}
	}
}

void CCocosItemList::AddItem(CCocosItem *pItem)
{
	addChild(pItem);
	m_listItems.push_back(pItem);

	m_listHeight += pItem->getContentSize().height;

	ResortItems();
}

void CCocosItemList::InsertItem(CCocosItem *pItem, unsigned int pos)
{
	if(pos >= m_listItems.size())
	{
		m_listItems.push_back(pItem);
	}
	else
	{
		ListItem::iterator iter = m_listItems.begin();

		while(pos > 0)
		{
			iter++;
			pos--;
		}
		m_listItems.insert(iter,  pItem);
	}

	addChild(pItem);

	m_listHeight += pItem->getContentSize().height;

	ResortItems();
}

bool CCocosItemList::ChangeItem(CCocosItem *pItem, unsigned int pos)
{
	if(m_listItems.size() <= pos)
	{
		return false;
	}

	ListItem::iterator iter = m_listItems.begin();

	unsigned int tempPos = pos;
	while(pos > 0)
	{
		iter++;
		pos--;
	}
	
	m_listHeight -= (*iter)->getContentSize().height;
	(*iter)->setIsVisible(false);
	(*iter)->SetIsEnable(false);
	
	m_qRemovedItems.push((*iter));
	m_listItems.erase(iter);
	InsertItem(pItem, tempPos);

	ResortItems();

	return true;
}

bool CCocosItemList::isContainsTouch(cocos2d::CCTouch *pTouch)
{
	return (cocos2d::CCRect::CCRectContainsPoint(
		cocos2d::CCRect(m_rc.origin.x, m_rc.origin.y, m_rc.size.width, m_rc.size.height) ,
		this->convertTouchToNodeSpace(pTouch)));
}

void CCocosItemList::onReceiveNewTouch()
{
	m_lastY = convertTouchToNodeSpace(m_pCurTouch).y;
	m_bMoving = false;
}

void CCocosItemList::onCurTouchMoving()
{
	if(!m_bMoving)
	{
		for(ListItem::iterator iter = m_listItems.begin(); iter != m_listItems.end(); iter++)
		{
			(*iter)->DisableTouches();
		}
		m_bMoving = true;
	}
	float curY = convertTouchToNodeSpace(m_pCurTouch).y;

	m_curSpeed = curY - m_lastY;
	m_lastY = curY;

	m_startY += m_curSpeed;

	ResortItems();
}


void CCocosItemList::BeginUpdate()
{
	schedule(schedule_selector(CCocosItemList::OnUpdateItems));
}

void CCocosItemList::OnUpdateItems(cocos2d::ccTime dt)
{

	if(m_qRemovedItems.size() != 0)
	{
		CCocosItem *pPopItem = m_qRemovedItems.front();
		m_qRemovedItems.pop();
		pPopItem->removeFromParentAndCleanup(true);
	}
	if(m_pCurTouch != NULL || m_curSpeed == 0)
	{
		return;
	}

	if(m_curSpeed > 0)
	{
		m_curSpeed -= 1;
	}
	else if(m_curSpeed < 0)
	{
		m_curSpeed += 1;
	}

	if(abs(m_curSpeed) <= 1)
	{
		m_curSpeed = 0;
	}

	m_startY += m_curSpeed;

	ResortItems();
}

void CCocosItemList::SetEnable(bool bEnable)
{
	
	for(ListItem::iterator iter = m_listItems.begin(); iter != m_listItems.end(); iter++)
	{
		(*iter)->SetIsEnable(bEnable);
	}
	
}